# Replication archive for: Alexander Coppock, Kimberly Gross, Ethan Porter, Emily Thorson, and Thomas J. Wood, 
# "Conceptual Replication of Four Key Findings about Factual Corrections and Misinformation during the 2020 US Election: Evidence from Panel-Survey Experiments." 
# Forthcoming in the British Journal of Political Science doi:10.1017/S0007123422000631

library(tidyverse)
library(magrittr)
library(estimatr)
library(metafor)
library(broom)
library(ggbeeswarm)

meta_trait_cates_w1_all_adj <- read_rds("fitted_models/meta_trait_cates_w1_all_adj.rds")
trait_cates_w1_all_adj <- read_rds("fitted_models/trait_cates_w1_all_adj.rds")
meta_trait_dic_w1_all_adj <- read_rds("fitted_models/meta_trait_dic_w1_all_adj")

t0 <- meta_trait_cates_w1_all_adj
t1 <- trait_cates_w1_all_adj
t2 <- meta_trait_dic_w1_all_adj

grp_frm <- c(
  "grp_polint",
  "grp_pk",
  "grp_crt",
  "grp_nfc",
  
  "grp_emotstab", 
  "grp_extraversion",
  "grp_agree",
  "grp_conscientiousness",
  "grp_openness"
)

grp_to <- c(
  "Political interest",
  "Political knowledge",
  "Cognitive Reflection Test",
  "Need for Cognition",
  
  "Personality - Emotional Stability",
  "Personality - Extraversion",
  "Personality - Agreeableness",
  "Personality - Conscientiousness",
  "Personality - Openness"
  )



t0$name %<>%
  plyr::mapvalues(grp_frm, grp_to) %>% 
  factor(grp_to)
t1$name %<>%
  plyr::mapvalues(grp_frm, grp_to) %>% 
  factor(grp_to)
t2$name %<>%
  plyr::mapvalues(grp_frm, grp_to) %>% 
  factor(grp_to)


t2$rfac <- t2$name  %>% 
  str_detect("Personality") %>% 
  ifelse(
    "Personality\ntraits",
    "Other\nheterogeneity\ndimensions"
  ) %>% 
  factor(
    c("Personality\ntraits",
      "Other\nheterogeneity\ndimensions")
  )

t1$rfac <- t1$name %>% 
  str_detect("Personality") %>% 
  ifelse(
    "Personality\ntraits",
    "Other\nheterogeneity\ndimensions"
    ) %>% 
  factor(
    c("Personality\ntraits",
      "Other\nheterogeneity\ndimensions")
    )

t0$rfac <- t0$name %>% 
  str_detect("Personality") %>% 
  ifelse(
    "Personality\ntraits",
    "Other\nheterogeneity\ndimensions"
  ) %>% 
  factor(
    c("Personality\ntraits",
      "Other\nheterogeneity\ndimensions")
  )

t0$cfac <- t0$type2 %>% 
  str_detect("control") %>% 
  ifelse(
    "Misinformation effects",
    "Correction effects"
  ) %>% 
  factor(
    c("Misinformation effects",
      "Correction effects")
  )

t1$cfac <- t1$term %>% 
  str_detect("control") %>% 
  ifelse(
    "Misinformation effects",
    "Correction effects"
  ) %>% 
  factor(
    c("Misinformation effects",
      "Correction effects")
    )


t2$cfac <- t2$term %>% 
  str_detect("control") %>% 
  ifelse(
    "Misinformation effects",
    "Correction effects"
  ) %>% 
  factor(
    c("Misinformation effects",
      "Correction effects")
  )

t0$yint <- t0$rfac %>% 
  equals(
    "Personality\ntraits"
  ) %>% 
  ifelse(-4, 0)

t1$yint <- t1$rfac %>% 
  equals(
    "Personality\ntraits"
  ) %>% 
  ifelse(-4, 0)

t2$yint <- t2$rfac %>%
  equals(
    "Personality\ntraits"
  ) %>%
  ifelse(-4, 0)

t2 %<>% 
  filter(
    p.value < .05
  ) %>% 
  # join the low value
  left_join(
    t0 %>%
      group_by(name, type2, rfac, cfac) %>% 
      summarize(
        xlo = estimate %>% min,
        xhi = estimate %>% max
        )
    ) %>% 
  ungroup %>% 
  mutate(
    mid = xhi %>% 
      add(xlo) %>% 
      divide_by(2),
    slab = c(
      "***", "**", "*", ""
      ) %>% 
      extract(
        p.value %>% 
          findInterval(
            c(-Inf, .001, .01, .05, Inf)
          )
      )
    )

t1 %>% 
  ggplot() +
  geom_vline(
    aes(
      xintercept = 0,
    ),
    linetype = "dashed", 
    size = .25
  ) +
  geom_quasirandom(
    aes(
      estimate, name, fill = value, group = value
    ),
    shape = 21,
    bandwidth = .0005,
    size = .65,
    groupOnX = F,
    dodge.width = .6,
    data = t1
  ) +
  geom_quasirandom(
    aes(
      estimate, name, color = value, group = value
    ),
    bandwidth = .0005,
    size = .4,
    groupOnX = F,
    dodge.width = .6,
    data = t1
  ) +
  geom_segment(
    aes(
      x = xlo, xend = xhi,
      y = name %>% as.numeric %>% add(yint) %>% add(.5),
      yend = name %>% as.numeric %>% add(yint) %>% add(.5)
      ),
    data = t2,
    size = .25,
    linetype = "dotted"
    ) +
  geom_segment(
      aes(
        x = xlo, xend = xlo,
        y = name %>% as.numeric %>% add(yint),
        yend = name %>% as.numeric %>% add(.5) %>% add(yint)
      ),
      data = t2,
      size = .25,
      linetype = "dotted"
    ) +
  geom_segment(
    aes(
      x = xhi, xend = xhi,
      y = name %>% as.numeric %>% add(yint),
      yend = name %>% as.numeric %>% add(.5) %>% add(yint)
    ),
    data = t2,
    size = .25,
    linetype = "dotted"
  ) +
  geom_label(
      aes(
        x = mid, label = slab,
        y = name %>%
          as.numeric %>%
          add(.465) %>% 
          add(yint)
        ),
      data = t2,
      fill = "grey98",
      label.size = 0,
      fontface = "bold",
      size = 3,
      label.padding = unit(0.125, "lines")
    ) +
  geom_point(
    aes(
      x = estimate, y = name, fill = value
    ),
    data = t0,
    shape = 21,
    size = 5.5,
    position = position_dodge(.65)
  ) +
  geom_linerange(
    aes(
      xmin = conf.low %>% subtract(.05), xmax = conf.high %>% add(.05), y = name, group = value
    ),
    size = .8,
    color = "black",
    data = t0,
    position = position_dodge(.65)
  ) +
  geom_linerange(
    aes(
      xmin = conf.low, xmax = conf.high, y = name, color = value
    ),
    data = t0,
    position = position_dodge(.65)
  ) +
  geom_point(
    aes(
      x = estimate, y = name, fill = value
    ),
    shape = 21,
    data = t0,  
    size = 5.4,
    position = position_dodge(.65)
  ) +
  geom_text(
    aes(
      estimate, name, label = estimate %>% round(1), group = value
    ),
    size = 1.4,
    color ="black",
    position = position_dodge(.65),
    data = t0
    ) +
  geom_text(
    aes(
      x, y, label = lab
    ),
    data = tribble(
      ~x,   ~y, ~lab,
      -6.6, 5.8, "Low on\nscale",
      -13.3, 5.8, "High on\nscale"
    ) %>%
      mutate(
        cfac = t0$cfac %>%
          levels %>%
          extract2(2) %>%
          factor(
            t0$cfac %>%
              levels
          ),
        rfac = "Personality\ntraits" %>%
          factor(
            t0$rfac %>%
              factor %>%
              levels
          )
      ),
    size = 2.5,
    lineheight = .7,
    fontface = 'italic'
  ) +
  facet_grid(
    rfac ~ cfac, 
    scales = "free", 
    space = "free"
  ) +
  scale_fill_grey(
    start = .6,
    end = .99
  ) +
  scale_color_grey(
    start = .6,
    end = .99
  ) +
  scale_y_discrete(
    expand = expansion(add = c(.75, 1.2)),
    breaks = t0$name %>% 
      levels,
    labels = t0$name %>% 
      levels %>% 
      str_remove("Personality - ")
  ) +
  scale_x_continuous(
    breaks = seq(-30, 30, 10)
  ) +
  labs(
    x = "", 
    y = ""
  ) 
  